import { useResponseSuccess } from '~/utils/response';

export default eventHandler(async (event) => {
  const query = getQuery(event);
  const { keyword, resourceType, status, page = 1, pageSize = 10 } = query;

  // 模拟可用资源数据
  const resources = [
    {
      id: '1',
      name: '豪华套房A',
      code: 'ROOM_A_001',
      resourceType: 'PHYSICAL',
      status: 'ACTIVE',
      capacity: 2,
      location: '1楼101室',
      categoryId: null,
      createTime: '2024-01-15 10:30:00',
    },
    {
      id: '2',
      name: '豪华套房B',
      code: 'ROOM_B_001',
      resourceType: 'PHYSICAL',
      status: 'ACTIVE',
      capacity: 2,
      location: '1楼102室',
      categoryId: null,
      createTime: '2024-01-15 09:15:00',
    },
    {
      id: '3',
      name: '温泉池1号',
      code: 'SPA_001',
      resourceType: 'PHYSICAL',
      status: 'ACTIVE',
      capacity: 10,
      location: '温泉区1号池',
      categoryId: null,
      createTime: '2024-01-15 08:45:00',
    },
    {
      id: '4',
      name: 'VIP包间1',
      code: 'VIP_001',
      resourceType: 'PHYSICAL',
      status: 'ACTIVE',
      capacity: 8,
      location: '2楼VIP区',
      categoryId: null,
      createTime: '2024-01-14 16:20:00',
    },
    {
      id: '5',
      name: '数字会议室A',
      code: 'DIGITAL_A_001',
      resourceType: 'DIGITAL',
      status: 'ACTIVE',
      capacity: 20,
      location: '线上会议室',
      categoryId: null,
      createTime: '2024-01-14 15:10:00',
    },
    {
      id: '6',
      name: '按摩服务1',
      code: 'MASSAGE_001',
      resourceType: 'SERVICE',
      status: 'ACTIVE',
      capacity: 1,
      location: '按摩区1号位',
      categoryId: null,
      createTime: '2024-01-14 14:30:00',
    },
    {
      id: '7',
      name: '餐厅包间A',
      code: 'DINING_A_001',
      resourceType: 'PHYSICAL',
      status: 'ACTIVE',
      capacity: 6,
      location: '餐厅1楼A区',
      categoryId: null,
      createTime: '2024-01-14 13:45:00',
    },
    {
      id: '8',
      name: '宠物医疗室1',
      code: 'PET_MED_001',
      resourceType: 'PHYSICAL',
      status: 'ACTIVE',
      capacity: 1,
      location: '宠物医疗区1号室',
      categoryId: null,
      createTime: '2024-01-14 12:20:00',
    },
  ];

  // 过滤数据
  let filteredData = resources;

  if (keyword) {
    filteredData = filteredData.filter(
      (item) =>
        item.name.includes(keyword as string) ||
        item.code.includes(keyword as string),
    );
  }

  if (resourceType) {
    filteredData = filteredData.filter(
      (item) => item.resourceType === resourceType,
    );
  }

  if (status) {
    filteredData = filteredData.filter((item) => item.status === status);
  }

  // 分页
  const start = (Number(page) - 1) * Number(pageSize);
  const end = start + Number(pageSize);
  const items = filteredData.slice(start, end);

  return useResponseSuccess({
    items,
    total: filteredData.length,
    page: Number(page),
    pageSize: Number(pageSize),
  });
});
